// ignore: avoid_web_libraries_in_flutter
import 'dart:html';
import 'dart:js';
import './dart_ui_fix.dart' as ui;
import 'package:flutter/material.dart';

// ignore: non_constant_identifier_names
int _frame_id = 1;

class IFrameWidget extends StatefulWidget {
  @override
  IFrameWidgetState createState() => IFrameWidgetState();
}

class IFrameWidgetState extends State<IFrameWidget> {
  final id = 'iframe' + (_frame_id++).toString();

  final iframe = IFrameElement();

  @override
  void initState() {
    ui.platformViewRegistry.registerViewFactory(id, (int viewId) {
      iframe
        ..id = id
        ..width = '100%'
        ..height = '100%'
        ..style.border = 'none';

      // Event Listener
      window.onMessage.forEach((element) {
        print('Event Received in callback: ${element.data}');
        if (element.data == 'MODAL_CLOSED') {
        } else if (element.data == 'SUCCESS') {
          print('PAYMENT SUCCESSFULL!!!!!!!');
        }
      });

      return iframe;
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return HtmlElementView(viewType: id);
  }
}
